<?php

namespace App\Admin\Controllers;

use App\Good;

use App\OrderPay;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class GoodsController extends Controller
{
    use ModelForm;

    public function good(){
        return Good::select('id','goods_name as text')->get();
    }
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('商品列表');
            $content->description('商品列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑商品');
            $content->description('编辑商品');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('增加商品');
            $content->description('增加商品');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Good::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->goods_name('商品名称');
            $grid->goods_thumbnail('缩略图')->display(function ($thumb) {
                return "<img src='$thumb' width='100px'>";
            });
            $grid->stock('库存');
            $grid->cost_price('首购价格');
            $grid->repeat_price('复购价格');
            $grid->is_recommend('在售/预售')->display(function($is_recommend){

             return    $is_recommend==1 ? '预售商品' : '正常商品';
            });
            $grid->sale_num('销售数量');
            $grid->is_sale('是否上架')->display(function ($is_sale) {
                return $is_sale == 0 ? '上架' : '下架';
            })->sortable();
            $grid->created_at('创建时间');
            $grid->disableExport();

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Good::class, function (Form $form) {
            $form->text('goods_name', '商品名称')->placeholder('请输入商品名称')->rules('required', [
                'required' => '请输入商品名称'
            ]);
            $form->image('goods_thumbnail', '商品缩略图')->removable()->rules('required|image', [
                '缩略图必须为图片'
            ]);
           /* $form->multipleImage('slide', '轮播图')->help('上传多张图片，请一次点选多张图片')->rules('required|image', [
                '上传类型为图片'
            ]);*/
            $form->currency('cost_price', '首购价格')->symbol('元')->rules('required', [
                'required' => '收购价格不能为空'
            ]);
            $form->currency('repeat_price', '复购价格')->symbol('元');
            $form->number('stock', '库存')->value(0);
            $form->timeRange('sale_start_time', 'sale_end_time', '销售时间段');
            $form->number('send_day','预计几天以后发货');
            $form->time('send_time','发货时间');
            $form->datetime('fixed_send_time','固定发货时间')->help('预售商品固定发货时间');

            $form->editor('detail', '商品详情');
            $states = [
                'on' => ['value' => 1, 'text' => '预售商品', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '在售商品', 'color' => 'success'],
            ];

            $form->switch('is_recommend', '是否预售')->states($states);
            $states1 = [
                'on' => ['value' => 1, 'text' => '下架', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '上架', 'color' => 'success'],
            ];

            $form->switch('is_sale', '是否上架')->states($states1);

        });
    }
}
